<?php
    session_start();		
	include("TableDataGateway/TDGAdministrarUsuarios.php");
	$TDG = new TDGAdministrarUsuarios();

	if (!empty($_POST['NumFilas'])){
		for ($i=0 ; $i<=$_POST['NumFilas'] ; $i++) {
			if (!empty($_POST[$i])){
				$TDG->actualizarPermisos($_POST['Email_'.$i], '-4');
			}
		}
	}
	
	if (!empty($_POST['NumFilas2'])){
		for ($i=0 ; $i<=$_POST['NumFilas2'] ; $i++) {
			if (!empty($_POST['Per_'.$i])){
				$TDG->actualizarPermisos($_POST['Email2_'.$i], $_POST['PermisoActual']);
			}
		}
	}
	
	if(!empty($_POST['Pendientes'])){
		$result = $TDG->seleccionarUsuarios("0");
		$numrows = mysql_num_rows($result);
	}else{
		if(!empty($_POST['Todo'])){
			$result = $TDG->seleccionarUsuarios("-1");
			$numrows = mysql_num_rows($result);
		}else{
			if(!empty($_POST['Tipo'])){
				$result = $TDG->seleccionarUsuarios($_POST['Tipos']);
				$numrows = mysql_num_rows($result);
			}
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Vitamina U</title>
        <meta name="keywords" content="itinerary, list" />
        <meta name="description" content="This page provides a list of all itineraries" />
        <link href="css/default.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
		<div id="wrapper">
        <?php include 'include/header2.php'; ?>
            <!-- end div#header -->
            <div id="page2">
                <div id="content">
                    <div id="welcome">
                        <center><h1>Administracion de Usuarios</h1>
							<form id="form_datos" action="administrarusuarios.php" method="POST">
								<input class="form_submitb" type="submit" name="Pendientes" id="Pendientes"  value="Ver nuevas solicitudes"/>
								<input class="form_submitb" type="submit" name="Todo" id="Todo"  value="Ver lista completa de usuarios"/><br/><br/>
								<H4>Seleccione un tipo de usuario</H4>
								<select name = "Tipos">																
									<option id="Visitante" name="Visitante" Value="1">Visitante</option>
									<option id="Docente" name="Docente" Value="2">Docente</option>
									<option id="Administrador" name="Administrador" Value="3">Administrador</option>									
								</select>
								<input class="form_submitb" type="submit" name="Tipo" id="Tipo"  value="Escoger"/>
							</form></center><br/>
							
							<?php if($numrows!=NULL){ ?>
								<table id="tablita" border="0" cellspacing="2" cellpadding="2">
								<tr>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Cedula</font></center></th>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Nombre</font></center></th>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Apellidos</font></center></th>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Telefono</font></center></th>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Email</font></center></th>
								<th><center><font color=#AA2808 face="Arial, Helvetica, sans-serif">Permisos</font></center></th>
								</tr>
								
								<?php $i=0;
								while ($i < $numrows) {
									
									$Cedula=mysql_result($result,$i,"Cedula");
									$Nombre=mysql_result($result,$i,"Nombre");
									$Apellidos=mysql_result($result,$i,"Apellidos");
									$Telefono=mysql_result($result,$i,"Telefono");
									$Email=mysql_result($result,$i,"Email");
									$Permisos=mysql_result($result,$i,"Permisos");?>

									<tr>
									<td><center><font face="Arial, Helvetica, sans-serif"><?php echo $Cedula; ?></font></center></td>
									<td><center><font face="Arial, Helvetica, sans-serif"><?php echo $Nombre; ?></font></center></td>
									<td><center><font face="Arial, Helvetica, sans-serif"><?php echo $Apellidos; ?></font></center></td>
									<td><center><font face="Arial, Helvetica, sans-serif"><?php echo $Telefono; ?></font></center></td>
									<td><center><font face="Arial, Helvetica, sans-serif"><?php echo $Email; ?></font></center></td>
									<td><center>
									<form id="eliminar_usuario" action="administrarusuarios.php" method="POST">							

										<select name = "PermisoActual">
										<option <?php if(($Permisos == '1')||($Permisos == '-1')){echo "Selected";} ?> Value='1'>Visitante</option>
										<option <?php if(($Permisos == '2')||($Permisos == '-2')){echo "Selected";} ?> Value='2'>Docente</option>
										<option <?php if(($Permisos == '3')||($Permisos == '-3')){echo "Selected";} ?> Value='3'>Administrador</option>

										<input class="form_submitb" type="submit" name="Per_<?php echo $i; ?>" id="Per_<?php echo $i; ?>" value="Modificar"/>
										<input type="hidden" name="Email2_<?php echo $i; ?>" id="Email2_<?php echo $i; ?>" value="<?php echo $Email; ?>" />
										<input type="hidden" name="NumFilas2" id="NumFilas2" value="<?php echo $numrows; ?>" />
										</select>
									</form>																											
									</center></td>
									<td>
									<form id="eliminar_usuario" action="administrarusuarios.php" method="POST">
										<input class="form_submitb" type="submit" name="<?php echo $i; ?>" id="<?php echo $i; ?>" value="Eliminar"/>
										<input type="hidden" name="Email_<?php echo $i; ?>" id="Email_<?php echo $i; ?>" value="<?php echo $Email; ?>" />
										<input type="hidden" name="NumFilas" id="NumFilas" value="<?php echo $numrows; ?>" />
									</form>
									</td>
									</tr>
									
									<?php $i++;
								} ?>
								
								</table>
							<?php }?>
                    </div>                   
                    <!-- end div#welcome -->			                   
                </div>
                <!-- end div#content -->
				
                <div style="clear: both; height: 1px"></div>
            </div>
            <?php include 'include/footer2.php'; ?>
        </div>
        <!-- end div#wrapper -->
    </body>
</html>